using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class TinhTrang_Phieu_DAL
    {
        private SqlConnection cn = null;
        public  TinhTrang_Phieu_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(TinhTrang_Phieu_DTO Tinhtrang_Phieu)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_Phieu_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_Phieu",Tinhtrang_Phieu .MaTinhTrang_Phieu   );
            cm.Parameters.AddWithValue("@TenTinhTrang",Tinhtrang_Phieu .TenTinhTrang );
                     
            cm.ExecuteNonQuery();
        }
       public void Update(TinhTrang_Phieu_DTO Tinhtrang_Phieu)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_Phieu_Upsate", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_Phieu", Tinhtrang_Phieu.MaTinhTrang_Phieu );
            cm.Parameters.AddWithValue("@TenTinhTrang", Tinhtrang_Phieu.TenTinhTrang);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_Phieu_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_Phieu", iD);
            cm.ExecuteNonQuery();
        }
       public TinhTrang_PhieuCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_Phieu_SET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            TinhTrang_PhieuCollection    TinhtrangphieuCollection = new TinhTrang_PhieuCollection   ();

            while (dr.Read ())
            {
                TinhTrang_Phieu_DTO tinhtrangphieu = new TinhTrang_Phieu_DTO   ();
                tinhtrangphieu .MaTinhTrang_Phieu   = int.Parse(dr.GetValue(0).ToString());
                tinhtrangphieu .TenTinhTrang   = dr.GetValue(1).ToString();

                TinhtrangphieuCollection.Add(tinhtrangphieu);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return TinhtrangphieuCollection ;
        }
    }
}
